# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from com.zzh.validation.getMsgCode import MessageCode
from com.zzh.dao.userDao import UserDao
from com.zzh.dao.unitDao import UnitDao
from com.zzh.dao.puserDao import PuserDao
from flask_cors import CORS
from flask_apidoc import ApiDoc

from . import app

CORS(app, supports_credentials=True)

'''用户表 zzh_user API'''
__author__ = "Prince CHEN"


@app.route('/is-regist', methods=['POST'])
def is_regist():
    """
    :return:
    """
    if not request.json:
        abort(400)
    # 首先判断手机号是否已被注册
    phone = request.json['phoneNum']

    res = UserDao().is_regist(phone)
    if res:
        # 若已被注册，返回-1
        return jsonify({"message": {"code": 0, "info": "该手机已被注册！"}})
    else:
        return jsonify({"message": {"code": 1, "info": "该手机未注册！"}})


@app.route('/regist-user', methods=['POST'])
def regist_user():
    if not request.json:
        abort(400)

    login_phone = request.json['phone']
    login_pwd = request.json['password']
    code = request.json['code']

    res = MessageCode().verifyCode(login_phone, code)

    if res['code'] == 200:
        regist_info = {"login_phone": login_phone, "login_pwd": login_pwd}
        res1 = UserDao().regist_user(regist_info)
        return jsonify({"message": {"code": 1, "info": "OK！"}, "new_id": res1})
    else:
        return jsonify({"message": {"code": 0, "info": "error！"}})


# ------------获取短信验证码--------------
@app.route('/get-msgcode', methods=['POST'])
def get_msgcode():
    if not request.json:
        abort(400)
    phone = request.json['phoneNum']
    res = MessageCode().send(phone)
    if res['code'] == 200:
        return jsonify({"message": {"code": 1, "info": "验证码已发送！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "验证码发送失败！"}})


# ---------检验短信验证码--------------
@app.route('/verify-msgcode', methods=['POST'])
def verify_code():
    if not request.json:
        abort(400)
    phone = request.json['phoneNum']
    code = request.json['code']
    res = MessageCode().verifyCode(phone, code)
    if res['code'] == 200:
        return jsonify({"message": {"code": 1, "info": "验证成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "验证失败！"}})


@app.route('/update-user/<int:user_id>', methods=['POST'])
def update_user(user_id):
    if not request.json:
        abort(400)
    oldInfo = UserDao().get_user_by_id(user_id)

    user_info = {
        "user_type": request.json.get('user_type', oldInfo[1]),
        "login_name": request.json.get('login_name', oldInfo[2]),
        "login_phone": request.json.get('login_phone', oldInfo[3]),
        "login_email": request.json.get('login_email', oldInfo[4]),
        "login_nickname": request.json.get('login_nickname', oldInfo[5])

    }
    result = UserDao().update_user(user_info, user_id)

    return jsonify({"userInfo": user_info, "message": {"code": 1, "info": "信息更新成功！"}})


@app.route('/user-login', methods=['POST'])
def user_login():
    if not request.json:
        abort(400)
    loginPhone = request.json['login_phone']
    password = request.json['login_pwd']
    code, user_id, user_type = UserDao().user_login(loginPhone, password)
    if code == 0:
        return jsonify({"message": {"code": 0, "info": "用户不存在！"}})
    elif code == -1:
        return jsonify({"message": {"code": -1, "info": "密码错误！"}})
    else:
        return jsonify({"message": {"code": 1, "info": "验证成功！"}, "userId": user_id, "user_type": user_type})


# -------------根据用户id获取用户信息------------
@app.route('/get-user-info/<int:user_id>', methods=['GET'])
def get_user_info(user_id):
    userInfo, user_type = UserDao().get_user_info(user_id)

    if int(user_type) == 1:
        pinfo = PuserDao().query_puser_info(user_id)
        return jsonify({"message": {"code": 1, "info": "OK！"}, "userInfo": userInfo, "moreInfo": pinfo})
    else:
        uInfo = UnitDao().query_unit_info(user_id)
        return jsonify({"message": {"code": 1, "info": "OK！"}, "userInfo": userInfo, "moreInfo": uInfo})


@app.route('/reset-passwd', methods=['POST'])
def reset_passwd():
    if not request.json:
        abort(400)
    phone = request.json['login_phone']
    newpasswd = request.json['newPassword']

    code = request.json['code']
    verify = MessageCode().verifyCode(phone, code)
    if verify['code'] == 200:
        rs = UserDao().reset_passwd(newpasswd, phone)
        return jsonify({"message": {"code": 1, "info": "密码已重置！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "验证码有误！"}})


# -----------------新增密码修改API    2017-3-23 -----------------
@app.route('/update-passwd/<int:user_id>', methods=['POST'])
def update_passwd(user_id):
    if not request.json:
        abort(400)
    oldPwd = request.json['oldPwd']
    newPwd = request.json['newPwd']
    rs = UserDao().update_passwd(user_id, oldPwd, newPwd)
    if rs == 1:
        return jsonify({"message": {"code": 1, "info": "更新成功"}})
    elif rs == -1:
        return jsonify({"message": {"code": 0, "info": "原始密码错误"}})
    else:
        return jsonify({"message": {"code": 0, "info": "更新失败"}})
